package extend_widget

import (
	"clientDemo/ui/fyne_ui/common"
	"clientDemo/ui/fyne_ui/widget"
	"clientDemo/ui/fyne_ui/widget/interface"
	"clientDemo/utils"
	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/canvas"
	"fyne.io/fyne/v2/dialog"
	"fyne.io/fyne/v2/driver/desktop"
	widget2 "fyne.io/fyne/v2/widget"
	"image/color"
)

var _ widgetcommon.IWidgetItem = (*EntryExt)(nil)

func init() {
	widget.RegWidget("WidgetEdit", "输入框1", NewEntry)
}

type EntryExt struct {
	*WidgetContainer
	//EntryExt *widget2.EntryExt
	Entry *MyEntry
	*Action
	//common.IUserInfo
	rootNode widgetcommon.IWidgetItem //父节点
}

func NewEntry(name string, skinInfo widgetcommon.IWidgetSkin, rootNode widgetcommon.IWidgetItem) widgetcommon.IWidgetItem {
	if skinInfo == nil {
		return nil
	}
	//o := widget2.NewEntry()
	o := NewMyEntry2()
	_entry := &EntryExt{
		Entry:  o,
		Action: &Action{},
		WidgetContainer: &WidgetContainer{
			Container: &Container{
				name: name,
			},
			Border: &canvas.Rectangle{
				FillColor:   color.Transparent,
				StrokeColor: color.RGBA{255, 0, 0, 255}, //黄色
				StrokeWidth: 2,
			},
		},
		rootNode: rootNode,
	}
	_entry.WidgetContainer.ac = _entry.Action
	_entry.IUserInfo = common.NewWidgetUserInfo(name, skinInfo.GetSkinName(), _entry)
	_entry.WidgetContainer.SetObjects([]fyne.CanvasObject{_entry.Entry})
	_entry.SetBase(_entry, rootNode)
	_entry.ExtendBaseWidget(_entry)
	_entry.Entry.ac = _entry.Action
	_entry.Entry.Text = name
	_entry.Entry.Disable() // 设置为不可编辑
	return _entry
}
func (e *EntryExt) ForEachProperty(cb func(k, v string)) {
	e.IUserInfo.ForEachProperty(func(key, value string) {
		cb(key, value)
	})
}

func (e *EntryExt) SetProperty(key, value string, isProperty bool) {
	e.WidgetContainer.SetProperty(key, value, isProperty)
	switch key {
	case "Scale":
		size := e.Entry.Size()
		scale := utils.ParseFloat32(value)
		newSize := fyne.NewSize(size.Width*scale, size.Height*scale)
		e.Entry.Resize(newSize)
	case "ShowTxt":
		e.Entry.SetText(value)
	case "FontSize":
	case "Color":
	case "Size":
		e.Entry.Resize(fyne.NewSize(300, 50)) // 设置容器大小
	}
}

type MyEntry struct {
	*widget2.Entry
	ac *Action
}

func NewMyEntry2() *MyEntry {
	o := &MyEntry{
		Entry: &widget2.Entry{Wrapping: fyne.TextWrap(fyne.TextTruncateClip)}, //widget2.NewEntry(),
	}
	o.ExtendBaseWidget(o.Entry)
	return o
}
func NewMyEntry() fyne.CanvasObject {
	o := &MyEntry{
		Entry: widget2.NewEntry(),
	}
	o.ExtendBaseWidget(o)
	return o
}
func (e *MyEntry) MouseIn(ev *desktop.MouseEvent) {

}
func (e *MyEntry) MouseMoved(ev *desktop.MouseEvent) {
	e.ac.MouseMoved(ev)
}
func (e *MyEntry) MouseOut() {

}

func (e *MyEntry) MouseDown(ev *desktop.MouseEvent) {
	e.ac.MouseDown(ev)
}

func (e *MyEntry) MouseUp(ev *desktop.MouseEvent) {
	e.ac.MouseUp(ev)
}
func (e *MyEntry) MinSize() fyne.Size {
	return e.Entry.MinSize()
}

func (e *MyEntry) DragEnd() {
	ttt := 1
	_ = ttt
}
func (e *MyEntry) Dragged(*fyne.DragEvent) {
	ttt := 1
	_ = ttt
}

func (e *MyEntry) MouseDrag(ev *desktop.MouseEvent) {
	dialog.ShowInformation("信息", "正在拖动", nil)
}
func (e *MyEntry) Tapped() {
	dialog.ShowInformation("信息", "输入框被点击了！", nil)
}

func (e *MyEntry) TappedSecondary() {
	dialog.ShowInformation("信息", "输入框被右击了！", nil)
}
